<script setup lang="ts">
import { computed, ref, onMounted } from 'vue'
import { useMemberStore } from '@/stores'
import { useSearchStore } from '@/stores/modules/search'
import { getStoreListAPI } from '@/services/store'
import { useStoreStore } from '@/stores/modules/store' // 引入门店 Store

// 获取门店信息
const storeList = ref([])
const getStoreData = async () => {
  try {
    const res = await getStoreListAPI()
    storeList.value = res.result

    // 如果有门店信息，设置第一个门店为默认选中
    if (storeList.value.length > 0) {
      storeStore.setCurrentStore(storeList.value[0])
    }
  } catch (error) {
    console.error('获取门店信息失败：', error)
  }
}

const memberStore = useMemberStore()
const { safeAreaInsets } = uni.getSystemInfoSync()

// 搜索方法
const navigatorToSearch = async () => {
  uni.navigateTo({
    url: '/pages/search/search',
  })
}

const popup = ref<{
  open: () => void
  close: () => void
}>()

// 获取门店 Store 实例
const storeStore = useStoreStore()

// 处理门店选择的函数
const handleStoreSelect = (store: StoreInfo) => {
  console.log('选中的门店名称:', store.name)
  // 将选中的门店信息存入 Pinia Store
  storeStore.setCurrentStore(store)
  popup.value?.close()
}

const selectAddress = () => {
  popup.value?.open()
}

// 计算属性：获取当前选中的门店名称
const currentStoreName = computed(() => {
  return storeStore.currentStore?.name || '暂无门店信息'
})

// 在组件挂载后调用获取门店信息函数
onMounted(() => {
  getStoreData()
})
</script>

<template>
  <view class="navbar" :style="{ paddingTop: safeAreaInsets!.top + 10 + 'px' }">
    <!-- logo文字 -->
    <view class="logo">
      <image class="logo-image" src="@/static/images/logo_row.png"></image>
      <text class="logo-text">新鲜 · 亲民 · 快捷</text>
    </view>

    <view class="location">
      <image class="location-image" src="@/static/tabs/location.png"></image>
      <text class="location-text">
        门店选择:
        {{ currentStoreName }}
      </text>
      <button class="location-button" @tap="selectAddress">选择配送点</button>
    </view>

    <!-- <view class="search">
      <view class="input" @tap="navigatorToSearch">
        <text class="iconfont icon-sousuo"></text>
        <text class="search-input" placeholder="搜索商品">搜索</text>
        <text class="icon-scan"></text>
      </view>
    </view> -->

    <uni-popup ref="popup" type="bottom" background-color="#fff">
      <view class="content">
        <!-- 使用 v-for 渲染门店信息 -->
        <view
          class="item"
          v-for="store in storeList"
          :key="store.id"
          @tap="handleStoreSelect(store)"
        >
          <view class="user">{{ store.name }}</view>
          <view class="address">{{ store.address }}</view>
          <text class="icon icon-ring"></text>
        </view>
      </view>
    </uni-popup>
  </view>
</template>

<style lang="scss">
.navbar {
  background-image: url(@/static/images/navigator_bg.png);
  background-size: cover;
  position: relative;
  display: flex;
  flex-direction: column;
  padding-top: 20px;

  .logo {
    display: flex;
    align-items: center;
    height: 64rpx;
    padding-left: 30rpx;

    .logo-image {
      width: 166rpx;
      height: 60rpx;
    }

    .logo-text {
      flex: 1;
      line-height: 28rpx;
      color: #fff;
      margin: 2rpx 0 0 20rpx;
      padding-left: 20rpx;
      border-left: 1rpx solid #fff;
      font-size: 26rpx;
    }
  }

  .content {
    min-height: 300rpx;
    max-height: 540rpx;
    overflow: auto;
    padding: 20rpx;
    .item {
      padding: 30rpx 50rpx 30rpx 60rpx;
      background-size: 40rpx;
      background-repeat: no-repeat;
      background-position: 0 center;
      background-image: url(https://pcapi-xiaotuxian-front-devtest.itheima.net/miniapp/images/locate.png);
      position: relative;
    }
    .icon {
      color: #999;
      font-size: 40rpx;
      transform: translateY(-50%);
      position: absolute;
      top: 50%;
      right: 0;
    }
    .icon-checked {
      color: #27ba9b;
    }
    .icon-ring {
      color: #444;
    }
    .user {
      font-size: 28rpx;
      color: #444;
      font-weight: 500;
    }
    .address {
      font-size: 26rpx;
      color: #666;
    }
  }

  .search {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 64rpx;
    margin: 16rpx 20rpx;
    padding: 0 10rpx;
    color: #fff;
    font-size: 28rpx;
    border-radius: 32rpx;
    background-color: rgba(255, 255, 255, 0.5);

    .input {
      flex: 1;
      height: 100%;

      font-size: 28rpx;
      display: flex;
      align-items: center;
      justify-content: space-between;

      .search-input {
        flex: 1;
        color: #fff;
      }

      .search-input::placeholder {
        color: #fff;
      }

      .icon-search {
        font-size: 30rpx;
        padding: 15rpx;
      }

      .icon-scan {
        font-size: 30rpx;
        padding: 15rpx;
      }
    }
  }

  /* 历史搜索 */
  .history-container {
    width: 100%;
    height: 300rpx;
    position: absolute;
    top: 310rpx;
    left: 0;

    background-color: #fff;
    z-index: 999;
    box-sizing: border-box;
    padding: 0 20rpx;

    .history-title {
      padding: 20rpx;
    }

    .history-containerp-content {
      width: 100%;
      display: flex;
      flex-wrap: wrap;

      .history-item {
        padding: 10rpx 20rpx;
        margin: 10rpx 20rpx;
        background-color: #f5f5f5;
        // border-radius: 10rpx;
      }
    }
  }

  /* 搜索结果 */
  .search-results {
    width: 100%;
    height: 300rpx;
    position: absolute;
    top: 310rpx;
    left: 0;

    background-color: #fff;
    z-index: 999;
    box-sizing: border-box;
    padding: 0 20rpx;

    .result-item {
      padding: 20rpx 0;
      border-bottom: 1rpx solid #f5f5f5;
    }
  }

  .icon-search {
    &::before {
      margin-right: 10rpx;
    }
  }

  .icon-scan {
    font-size: 30rpx;
    padding: 15rpx;
  }

  .location {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    padding: 0 10rpx 0 26rpx;
    height: 44rpx;
    margin: 16rpx 20rpx 0rpx 20rpx;
    color: #474444;
    font-size: 28rpx;
    border-radius: 10rpx 10rpx 0rpx 0rpx;
    background-color: rgba(255, 255, 255, 0.5);

    .location-button {
      display: flex;
      align-items: center;
      justify-content: center;
      height: 80%;
      margin-right: 0rpx;
      font-size: 28rpx;
      border-radius: 50rpx;
      color: #ffffff;
      background-color: #28bb9c;
    }

    .location-image {
      width: 5%;
      height: 75%;
    }

    .location-text {
      margin-left: 10rpx;
    }
  }
}
</style>
